<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_CustomerTotal extends Zend_Db_Table{
	
	protected $_name    = "customer_total";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->insert('customer_total', $data);
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function getDatas(){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('ct' => 'customer_total'), 'sum(total) as total')
                    ->joinRight(array('c' => 'customers'), 'c.id = ct.customer_id', array('id as customer_id', 'name'))
                    ->group('c.id')
                    ->order("c.id");
                
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($customer_id){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        $select = $db->select()
                    ->from(array('ct' => 'customer_total'), array('id', 'total'))
                    ->order('ct.id DESC')
                    ->where('ct.customer_id = ?', $customer_id);
                
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->delete("customer_total", "id = $id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
}